package boundary;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import javax.swing.JTextField;
import java.awt.Font;
import org.jdesktop.swingx.JXTitledSeparator;
import dataview.TipoTermine;
import exceptions.DBException;
import store.Terminologia;
import store.TuttiCibi;

import java.awt.Color;
import java.util.List;
import java.util.Set;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import java.awt.Cursor;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;

public class NuovoCibo extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	private JLabel titolo = null;
	private JLabel nomeL = null;
	private JTextField nomeT = null;
	private JButton jButton = null;
	private Home main;
	
	/**
	 * This is the default constructor
	 */
	public NuovoCibo(Home home) {
		super();
		this.main=home;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints.gridy = 2;
		GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
		gridBagConstraints31.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints31.gridx = 1;
		gridBagConstraints31.gridy = 1;
		gridBagConstraints31.weightx = 1.0;
		gridBagConstraints31.insets = new Insets(5, 5, 5, 5);
		GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
		gridBagConstraints21.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints21.gridy = 1;
		gridBagConstraints21.anchor = GridBagConstraints.EAST;
		gridBagConstraints21.gridx = 0;
		GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
		gridBagConstraints12.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints12.gridy = 0;
		gridBagConstraints12.fill = GridBagConstraints.VERTICAL;
		gridBagConstraints12.anchor = GridBagConstraints.CENTER;
		gridBagConstraints12.gridwidth = 2;
		gridBagConstraints12.gridx = 0;
		nomeL = new JLabel();
		nomeL.setText("Nome");
		nomeL.setFont(new Font("Dialog", Font.BOLD, 12));
		titolo = new JLabel();
		titolo.setText("Inserisci nuovo cibo");
		titolo.setFont(new Font("Dialog", Font.BOLD, 20));
		this.setLayout(new GridBagLayout());
		this.setSize(273, 117);
		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		this.add(titolo, gridBagConstraints12);
		this.add(nomeL, gridBagConstraints21);
		this.add(getNomeT(), gridBagConstraints31);
		this.add(getJButton(), gridBagConstraints);
	}

	/**
	 * This method initializes nomeT
	 * 
	 * @return javax.swing.JTextField
	 */
	public JTextField getNomeT() {
		if (nomeT == null) {
			nomeT = new JTextField();
			nomeT.setColumns(20);
			nomeT.setFont(new Font("Dialog", Font.PLAIN, 12));
			nomeT.setActionCommand("buttonPressed");
			nomeT.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent k) {
					if (nomeT.getText().length() == 0)
						getJButton().setEnabled(false);
					else if (!getJButton().isEnabled())
						getJButton().setEnabled(true);
				}
			});
			nomeT.addActionListener(this);
		}
		return nomeT;
	}
	
	private JPanel getPanel() {
		return this;
	}

	

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Inserisci");
			jButton.setFont(new Font("Dialog", Font.BOLD, 12));
			jButton.setIcon(new ImageIcon(getClass().getResource("/icone/application-plus-edit-icon.png")));
			jButton.setHorizontalTextPosition(SwingConstants.TRAILING);
			jButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			jButton.setEnabled(false);
			jButton.addActionListener(this);
		}
		return jButton;
	}
	
	public void focus() {
		nomeT.requestFocus();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String nome = nomeT.getText();
		if (nome.length() > 0) {
			/*
			try {
				if (TuttiCibi.getTuttiCibi().presente(nome))
					JOptionPane.showMessageDialog(getPanel(), "Cibo gia` presente", "Errore", JOptionPane.ERROR_MESSAGE);
				else
					main.richiediInfoCibo(nome);
			} catch (HeadlessException e) {
				e.printStackTrace();
			} catch (DBException e) {
				e.printStackTrace();
			}
			*/

			main.inserireCibo(nome);
		}
		else
			JOptionPane.showMessageDialog(getPanel(), "Inserire un nome", "Errore", JOptionPane.ERROR_MESSAGE);
	}

} // @jve:decl-index=0:visual-constraint="20,36"
